package sprinklerbee.views;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import sprinklerbee.factory.ServiceFactory;
import sprinklerbee.model.Scheduler;
import sprinklerbee.utils.MemoryCache;


@SuppressWarnings("serial")
public class ManagePanel extends JPanel {

	private JButton btn_off;
	private JButton btn_on;
	private JButton btn_chart;
	
	@SuppressWarnings("unused")
	public ManagePanel(){
		
		// Control Panel
		final JPanel controlPanel = new JPanel();
		setLayout(new GridLayout(1, 3 ,2, 5));
		setBackground(Color.gray); 
		
		btn_on = new JButton();
		btn_on.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btn_off.setEnabled(true);
				btn_on.setEnabled(false);
				Scheduler scheduler=new Scheduler();
				scheduler.setStatus("Active");
				ServiceFactory.getSchedulerServiceInstance().updateAllSchedule(scheduler);
				MemoryCache.refreshMemoryCache();
				ScheduleSettingPanel.refreshSchedulerPanel();
			}
		});
		btn_on.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/turnOn.png")));   
		btn_on.setBorderPainted(false);   
		btn_on.setFocusPainted(false);   
		btn_on.setContentAreaFilled(true); 
		
		btn_off = new JButton();
		btn_off.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int i = JOptionPane.showConfirmDialog(null,
						"Are you sure to deactivate the system?", "Tip", 0);
				if (i == 0) {
					btn_off.setEnabled(false);
					btn_on.setEnabled(true);
					Scheduler scheduler=new Scheduler();
					scheduler.setStatus("Unactive");
					ServiceFactory.getSchedulerServiceInstance().updateAllSchedule(scheduler);
					MemoryCache.refreshMemoryCache();
					ScheduleSettingPanel.refreshSchedulerPanel();
				}
			}
		});
		btn_off.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/turnOff.png")));   
		btn_off.setBorderPainted(false);   
		btn_off.setFocusPainted(false);   
		btn_off.setContentAreaFilled(true);
		// Chart*/
		
		btn_chart = new JButton();
		btn_chart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ConsumptionDialog panel=new ConsumptionDialog();
				panel.setVisible(true);
				controlPanel.setEnabled(false);
			}
		});
		btn_chart.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/pie_chart.jpg")));   
		btn_chart.setBorderPainted(false);   
		btn_chart.setFocusPainted(false);   
		btn_chart.setContentAreaFilled(true); 
		
		add(btn_on);
		add(btn_off);
		add(btn_chart);
	}
}
